#include <atlbase.h>
#include "../base/basictypes.h"
#include "../base/logging.h"
#include "pcagent_idl_datax.h"
#include "com_proxy.h"

#if IS_MACHINE_HANDLER
OBJECT_ENTRY_AUTO(__uuidof(LongkeyComProxyMachineClass), ComProxy)
#else
OBJECT_ENTRY_AUTO(__uuidof(LongkeyComProxyUserClass), ComProxy)
#endif

namespace
{
	class LongkeyPCAgentPSModule : public CAtlDllModuleT<LongkeyPCAgentPSModule>
	{
	public:
		LongkeyPCAgentPSModule() {}

	private:
		DISALLOW_EVIL_CONSTRUCTORS(LongkeyPCAgentPSModule);
	} _AtlModule;
}


BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
{
	HRESULT hr = _AtlModule.DllMain(reason, reserved);
	if (FAILED(hr))
		return hr;

	return PrxDllMain(instance, reason, reserved);
}

STDAPI DllCanUnloadNow()
{
	if (_AtlModule.DllCanUnloadNow() == S_OK &&
		PrxDllCanUnloadNow() == S_OK)
		return S_OK;

	return S_FALSE;
}

STDAPI DllGetClassObject(__in REFCLSID rclsid, __in REFIID riid, LPVOID FAR* ppv)
{
	HRESULT hr_atl = _AtlModule.DllGetClassObject(rclsid, riid, ppv);
	if (SUCCEEDED(hr_atl))
		return hr_atl;

	HRESULT hr_prx = PrxDllGetClassObject(rclsid, riid, ppv);
	if (FAILED(hr_prx))
		CORE_LOG(LE, (_T("[DllGetClassObject failed][%s][%s][0x%x][0x%x]"),
			GuidToString(rclsid), GuidToString(riid), hr_atl, hr_prx));
	return hr_prx;
}

STDAPI DllRegisterServer()
{
	HRESULT hr = _AtlModule.DllRegisterServer(FALSE);
	if (FAILED(hr))
	{
		CORE_LOG(LE, (_T("[DllRegisterServer failed][0x%x]"), hr));
		return hr;
	}
	return PrxDllRegisterServer();
}

STDAPI DllUnregisterServer()
{
	HRESULT hr = PrxDllUnregisterServer();
	if (FAILED(hr))
	{
		CORE_LOG(LE, (_T("[DllUnregisterServer failed][0x%x]"), hr));
		return hr;
	}
	return _AtlModule.DllUnregisterServer(FALSE);
}